package com.mozz.finally_work.dao;

import com.mozz.finally_work.pojo.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

public interface UserDao {

    /*添加用户*/
    @Insert("INSERT INTO user (id,nick_name,email,password) values(#{id},#{nickName},#{email},#{password})")
    Integer addUser(User user);

    /*用户登录校验*/
    @Select("SELECT * FROM user where nick_name=#{nickName} and password=#{password}")
    @Results(value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "nick_name", property = "nickName"),
            @Result(property = "subscribeCount", column = "id", one = @One(select = "com.mozz.finally_work.dao.SubscribeDao.countUserSubscribe", fetchType = FetchType.EAGER)),
            @Result(property = "blogCount", column = "id", one = @One(select = "com.mozz.finally_work.dao.BlogDao.countBlog", fetchType = FetchType.EAGER)),
            @Result(property = "collectionCount", column = "id", one = @One(select = "com.mozz.finally_work.dao.CollectionDao.countCollection", fetchType = FetchType.EAGER)),
            @Result(property = "categories", column = "id", many = @Many(select = "com.mozz.finally_work.dao.CategoryDao.queryALlCategoryByUserIdWithCount"))
    })
    User login(@Param("nickName") String nickName, @Param("password") String password);

    /*根据id查询用户*/
    @Select("SELECT user.*, count(*) as subscribeCount FROM user where user.id=#{id} ")
    @Results(value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "nick_name", property = "nickName"),
            @Result(property = "subscribeCount", column = "id", one = @One(select = "com.mozz.finally_work.dao.SubscribeDao.countUserSubscribe", fetchType = FetchType.EAGER)),
            @Result(property = "blogCount", column = "id", one = @One(select = "com.mozz.finally_work.dao.BlogDao.countBlog", fetchType = FetchType.EAGER)),
            @Result(property = "collectionCount", column = "id", one = @One(select = "com.mozz.finally_work.dao.CollectionDao.countCollection", fetchType = FetchType.EAGER)),
            @Result(property = "categories", column = "id", many = @Many(select = "com.mozz.finally_work.dao.CategoryDao.queryALlCategoryByUserIdWithCount", fetchType = FetchType.EAGER))
    })
    User findUserById(String id);


    /*修改用户信息*/
    @Update({"<script>",
            "update user",
            "<set>",
            "<if test=\"nickName != null \">nick_name = #{nickName}, </if>",
            "<if test=\"password != null\">password= #{password}, </if>",
            "<if test=\"email != null\">email= #{email}, </if>",
            "<if test=\"avatar != null\">avatar= #{avatar}</if>",
            "</set>",
            "where id=#{id}",
            "</script>"})
    Integer updateUser(User user);
}
